//
//  TypePicker.m
//  YaoHeAPP
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "TypePicker.h"

@implementation TypePicker

#define App_Frame_Height        [UIScreen mainScreen].bounds.size.height
#define App_Frame_Width         [[UIScreen mainScreen] bounds].size.width
#define Line_Low_Color   [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1]
//红色
#define  RedColor  [UIColor colorWithRed:216.0/255.0 green:12.0/255.0 blue:24.0/255.0 alpha:1]

+(void)showPickerView:(UIView *)selfView withTitle:(NSString*)titleString andSelectBlock:(void (^)(NSString *))selectRow
{
    //创建pickerView
    TypePicker*pickerView=[[TypePicker alloc]initWithFrame:selfView.bounds];
    pickerView.SelectBlock=selectRow;
    pickerView.titleStr=titleString;
    [pickerView setSubviews];
    [selfView addSubview:pickerView];
}
//初始化大小
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfAction)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self performSelector:@selector(showSelfAction) withObject:nil afterDelay:0];
    return self;
}

//设置，要弹出的视图上显示的东西
-(void)setSubviews
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, App_Frame_Width, 260)];
    _bgView.backgroundColor = Line_Low_Color;
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = .3;
    _bgView.layer.shadowOffset = CGSizeMake(0, -1);
    [self addSubview:_bgView];
    //标签
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake((App_Frame_Width - 140)/2, 0, 140, 40)];
    titleL.backgroundColor = [UIColor clearColor];
    titleL.font = [UIFont systemFontOfSize:17];
    titleL.textColor = [UIColor whiteColor];
    titleL.text = self.titleStr;
    [_bgView addSubview:titleL];
    //确定按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.backgroundColor = [UIColor clearColor];
    confirmBtn.layer.cornerRadius = 4;
    confirmBtn.layer.borderWidth = 0.5;
    confirmBtn.layer.borderColor = RedColor.CGColor;
    confirmBtn.frame = CGRectMake(App_Frame_Width - 105, 7, 90, 32);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:RedColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:confirmBtn];

    [Controller requestWithURL:@"AppPurchaseInfo/PostMatericalInfo?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
        NSMutableArray*dataMtArry=classData.data;
        self.dataArray=dataMtArry;
        _tdataArray=_dataArray[_rowTeger][@"CONDITION_ITEMS"];
        str1=[NSString string];
        str2=[NSString string];
        
        //初始化pickerView，设置数据来源
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, App_Frame_Width, 216)];
        _pickerView.backgroundColor = [UIColor whiteColor];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        [_bgView addSubview:_pickerView];
        }
        else{ALERTSHOW(classData.message)}
    }];
    
    
}
//点击确定按钮执行的方法
- (void)confirmBtnAction
{
//    if (str1==nil)
//    {
//        str1=_dataArray[0][@"CONDITION_NAME"];
//    }
    if (str2.length==0)
    {
        str2=_tdataArray[0][@"CONDITION_NAME"];
    }
    _stringRow=str2;
    self.SelectBlock(_stringRow);
    [self removeSelfAction];
}
//展示出选择器
- (void)showSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0, -260);
    } completion:^(BOOL finished) {
        
    }];
}
//移除选择器
- (void)removeSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform  = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

#pragma mark----------------选择器的代理方法
//返回每行的高度
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
{
    return 40;
}
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0)
    {
        return _dataArray.count;
    }
    return _tdataArray.count;
}

//自己定义选择器上现实的东西
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    //创建一个View，在view上放置一个label，这样可以更改字体，颜色，等属性，view上还可以放置其他视图
    if (component==0)
    {
        UIView *myView = [[UIView alloc]init];
        myView.frame =CGRectMake(0.0f, 0.0f, pickerView.frame.size.width, 40);
        UILabel *txtlabel = [[UILabel alloc] init];
        txtlabel.textAlignment = NSTextAlignmentCenter;
        txtlabel.frame = myView.frame;
        txtlabel.text=_dataArray[row][@"CONDITION_NAME"];
        self.rowTeger=row;
        [myView addSubview:txtlabel];
        return myView;
    }
    UIView *myViewt=[[UIView alloc]init];
    myViewt.frame =CGRectMake(0, 0.0f, pickerView.frame.size.width, 40);
    UILabel *txtlabel2 = [[UILabel alloc] init];
    txtlabel2.textAlignment = NSTextAlignmentCenter;
    txtlabel2.frame = myViewt.frame;
    txtlabel2.text=_tdataArray[row][@"CONDITION_NAME"];
    [myViewt addSubview:txtlabel2];
    return myViewt;
}

//获取当前选中的是哪一行（用block回调出去）
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
{
    if (component==0)
    {
        str1=_dataArray[row][@"CONDITION_NAME"];
        _tdataArray=_dataArray[row][@"CONDITION_ITEMS"];
        [_pickerView reloadComponent:1];
        [_pickerView selectRow:0 inComponent:1 animated:YES];
    }
    if (component==1)
    {
        str2=_tdataArray[row][@"CONDITION_NAME"];
    }
    
}

#pragma tap  delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_bgView.frame, point)) {
        return NO;
    }
    return YES;
}


@end


